<?php
/**
 *
 * @copyright	Copyright 2009-2012   庆丰裕软件工作室
 * @author	Zheng HanWei <315933614@qq.com
 * @link	http://www.qfyu.net/
 *  
 * */
if ($_POST['Submit']) {

    $fname = time() . rand(1000, 9999);

    //上传电子书封面
    if ($_FILES["cover"]["name"]) {
        if(!empty($_POST['cover']))
		{
			$file=$config['webroot'] . '/uploadfile/text/img/'.$_POST['cover'];	
			@unlink($file);
		}
        if ((($_FILES["cover"]["type"] == "image/gif")
                || ($_FILES["cover"]["type"] == "image/jpeg")
                || ($_FILES["cover"]["type"] == "image/pjpeg")
                || ($_FILES["cover"]["type"] == "image/png"))
                && ($_FILES["cover"]["size"] < 200000)) {
            $path = $config['webroot'] . '/uploadfile/text/img/';
            $imgname = $fname . '.' . end(explode('.', $_FILES["cover"]["name"]));

            if (file_exists($path . $imgname)) {
                echo "<script type='text/javascript'>alert('同名封面已经存在！');history.go(-1);</script>";
            } else {
                move_uploaded_file($_FILES["cover"]["tmp_name"], $path . $imgname);
            }
        } else {
            echo "<script type='text/javascript'>alert('封面格式不对或太大');history.go(-1);</script>";
        }
    }


    //上传电子书文件
    if ($_FILES["text"]["name"]) {
         if(!empty($_POST['source']))
		{
			$file=$config['webroot'] . '/uploadfile/text/'.$_POST['source'];	
			@unlink($file);
		}
        if ((($_FILES["text"]["type"] == "text/plain")
                || ($_FILES["text"]["type"] == "application/octet-stream")
                || ($_FILES["text"]["type"] == "application/pdf")
                || ($_FILES["text"]["type"] == "application/vnd.ms-powerpoint")
                || ($_FILES["text"]["type"] == "application/zip")
                || ($_FILES["text"]["type"] == "application/ocelet-stream")
                || ($_FILES["text"]["type"] == "application/msword"))
                && ($_FILES["text"]["size"] < 50000000)) {
            $path = $config['webroot'] . '/uploadfile/text/';
            $tname = $fname . '.' . end(explode('.', $_FILES["text"]["name"]));
            if (file_exists($path . $tname)) {
                echo "<script type='text/javascript'>alert('同名电子书已经存在！');history.go(-1);</script>";
            } else {
                move_uploaded_file($_FILES["text"]["tmp_name"], $path . $tname);
            }
        } else {
            echo "<script type='text/javascript'>alert('电子书格式不对，或太大！');history.go(-1);</script>";
        }
    }
    if (empty($_POST['vid'])) {

        $time = time();
        $sql = "insert into " . TRAINTXT . " (title,cat,smalltext,cover,source,detail,addtime) 
              values('$_POST[title]','$_POST[cat]','$_POST[smalltext]','$imgname','$tname','$_POST[detail]','$time')";
        $db->query($sql);
        echo "<script>location.href='module.php?m=train&s=train_text_list.php';</script>";
    } else {
        if(!empty($_POST['cover']) && empty($imgname)){
            $imgname=$_POST['cover'];
        }
        if(!empty($_POST['source']) && empty($tname)){
            $tname=$_POST['source'];
        }
        
        $sql="update " . TRAINTXT . " set title='$_POST[title]',cat='$_POST[cat]',smalltext='$_POST[smalltext]',cover='$imgname',source='$tname',detail='$_POST[detail]' where id=".$_POST['vid'];
        $db->query($sql);
        echo "<script>location.href='module.php?m=train&s=train_text_list.php';</script>";
    }
}

$sql="select * from ".TRAINCATS." where fid=0 order by sort asc";
$db->query($sql);
$cat=$db->getRows();
foreach($cat as $k=>$v){
    $sql="select * from ".TRAINCATS." where fid=".$v['id'];
    $db->query($sql);
    $cat[$k]['scat']=$db->getRows();
}

if ($_GET['id']) {
    $sql = "select * from " . TRAINTXT . " where id=$_GET[id]";
    $db->query($sql);
    $res = $db->fetchRow();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE><?php echo lang_show('admin_system');?></TITLE> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="main.css" rel="stylesheet" type="text/css" />
</HEAD>
<body>
<div class="guidebox">
    培训电子书添加
</div>
<form method="post" action="" enctype="multipart/form-data">
<div class="bigbox">
	<div class="bigboxhead">培训电子书添加</div>
	<div class="bigboxbody">
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
             <tr>
               <td width="10%">电子书标题</td>
               <td><input type="text" name="title" value="<?php echo $res['title'];?>" size="60"/></td>
             </tr>
              <tr>
               <td>电子书分类</td>
               <td>
                   <select name="cat">
                       <?php foreach($cat as $val){?>
                       <option <?php if($res['cat']==$val['id'])echo"selected";?> value="<?php echo $val['id']?>"><?php echo $val['name']?></option>
                       <?php foreach($val['scat'] as $list){?>
                       <option <?php if($res['cat']==$list['id'])echo"selected";?> value="<?php echo $list['id']?>">|__<?php echo $list['name']?></option>
                       <?php }?>
                       <?php }?>
                   </select>
               </td>
            </tr>
              <tr>
               <td>电子书简介</td>
               <td>
                   <textarea name="smalltext" cols="57"><?php echo $res['smalltext'];?></textarea>
               </td>
            </tr>
              <tr>
               <td>电子书封面</td>
               <td>
                   <img width="100" src="<?php echo $config['weburl'].'/uploadfile/text/img/'.$res['cover']?>">
                       <input type="hidden" name="cover" value="<?php echo $res["cover"]; ?> "><br/>
                   <input type="file" name="cover"/><span style="color: red;padding: 0 5px;">图片不能大于200K</span>
               </td>
            </tr>
             <tr>
               <td>选择电子书文件</td>
               <td>
                   <input type="hidden" name="source" value="<?php echo $res["source"]; ?> ">
                   <input type="file" name="text"/><span style="color: red;padding: 0 5px;">支持 压缩包、txt、doc、ppt、pdf、chm等常用格式，大小不能超过50M</span>
               </td>
            </tr>
              <tr>
               <td>电子书描述</td>
               <td>
                   <?php
			if(empty($res["detail"]))
				$res["detail"]=NULL;
			$BasePath = "../lib/fckeditor/";
			include($BasePath."fckeditor.php");	
			$fck_en = new FCKeditor('detail') ;
			if($config['language']=='cn')
				$fck_en->Config['DefaultLanguage']='zh-cn';
			else
			$fck_en->Config['DefaultLanguage']='en';
			$fck_en->BasePath    = $BasePath ;
			$fck_en->ToolbarSet  = 'Default' ;
			$fck_en->Width='100%';
			$fck_en->Height=500;
			$fck_en->Config['ToolbarStartExpanded'] = true;
			$fck_en->Value = stripslashes($res["detail"]);
			echo $fck_en->CreateHtml();
		  ?>
               </td>
            </tr>
             <tr>
               <td>&nbsp;</td>
               <td>
                   <input type="hidden" value="<?php echo $res["id"];?>" name="vid"/>
                   <input class="btn" type="submit" name="Submit" value="提交"/>
               </td>
             </tr>
           </table>
	</div>
</div>
</form>
</body>
</html>

